﻿using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Ymatou.Evaluate.Common.Cache
{
    public class RedisCacheManagerInstance
    {
        /// <summary>
        /// Redis哨兵集群
        /// </summary>
        static string[] EvaluateRedisGroupSentinelIps
        {
            get { return (AppSettingHelper.GetAppSetting("EvaluateRedisGroupSentinelIps", "") ?? "").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); }
        }

        /// <summary>
        /// Redis哨兵名称
        /// </summary>
        static string EvaluateRedisGroupSentinelName
        {
            get { return AppSettingHelper.GetAppSetting("EvaluateRedisGroupSentinelName", "group2"); }
        }

        public static IRedisClient DefaultRedisCache
        {
            get
            {
                var sentinel = new RedisSentinel(EvaluateRedisGroupSentinelIps, EvaluateRedisGroupSentinelName);
                var pool = (PooledRedisClientManager)sentinel.Setup();
                return pool.GetClient();
            }
        }
    }

    public class RedisCacheManager
    {
        static string GetKey(string key)
        {
            return AppSettingHelper.GetAppSetting("CachePrefix", "ProdEval") + "_" + key;
        }

        /// <summary>
        /// Redis存储条数
        /// </summary>
        static int ListCount
        {
            get { return EvaluateConfig.FirstPageEvaluateCount; }
        }

        #region Redis操作

        /// <summary>
        /// 获取Redis缓存
        /// </summary>
        /// <returns></returns>
        public static long GetListCount(string key)
        {
            try
            {
                key = GetKey(key);
                return RedisCacheManagerInstance.DefaultRedisCache.GetListCount(key);
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 获取Redis缓存
        /// </summary>
        /// <returns></returns>
        public static List<string> GetListCache(string key, bool isTop = true)
        {
            try
            {
                key = GetKey(key);
                var data = RedisCacheManagerInstance.DefaultRedisCache.GetAllItemsFromList(key);
                if (data != null)
                {
                    data.Reverse();
                    if (isTop)
                        return data.Take(ListCount).ToList();
                    else
                        return data;
                }
            }
            catch
            {

            }
            return null;
        }

        /// <summary>
        /// 添加Redis缓存
        /// </summary>
        public static void AddListCache(string key, string value, bool isTop = true)
        {
            try
            {
                key = GetKey(key);
                if (value == null) return;
                if (isTop)
                {
                    var rows = RedisCacheManagerInstance.DefaultRedisCache.GetListCount(key);
                    if (rows == ListCount)
                    {
                        RedisCacheManagerInstance.DefaultRedisCache.RemoveStartFromList(key);
                    }
                }
                RedisCacheManagerInstance.DefaultRedisCache.PushItemToList(key, value);
            }
            catch
            {
                return;
            }
        }

        /// <summary>
        /// 移除数据
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="isAll"></param>
        public static void RemoveListCache(string key, string value, bool isAll = false)
        {
            key = GetKey(key);
            if (isAll)
            {
                RedisCacheManagerInstance.DefaultRedisCache.RemoveAllFromList(key);
            }
            else
            {
                RedisCacheManagerInstance.DefaultRedisCache.RemoveItemFromList(key, value);
            }
        }

        #endregion
    }
}
